import { Injectable } from '@angular/core';
import * as gc from 'src/app/global/const';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
import { JsonResult } from '../model/JsonResult';
import { UUID } from 'angular2-uuid';

@Injectable({
  providedIn: 'root'
})
export class ContactCompanyService {

  
  private path_pre = 'contactCompany';

  private url = {
    'deleteByPrimaryKey':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/deleteByPrimaryKey',
    'selectByPrimaryKey':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/selectByPrimaryKey',
    'selectAll':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/selectAll',
    'saveOne':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'insert':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/insert',
    'update':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/update',
    'selectByType':gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/selectByType',
  }
  constructor(
    private http:HttpClient
  ) { }

  selectByType(typeSid:string):Observable<JsonResult>{
    const url = this.url.selectByType + '/' + typeSid;
    return this.http.get<JsonResult>(url);
  }

  deleteByPrimaryKey(iid:number):Observable<JsonResult>{
    const url = this.url.deleteByPrimaryKey + '/' + iid;
    return this.http.delete<JsonResult>(url);
  }

  selectByPrimaryKey(iid:number):Observable<JsonResult>{
    const url = this.url.selectByPrimaryKey + '/' + iid;
    return this.http.get<JsonResult>(url);
  }

  selectAll():Observable<JsonResult>{
    console.log(this.url.selectAll);
    return this.http.get<JsonResult>(this.url.selectAll);
  }

  saveOne(record:ContactCompany):Observable<JsonResult>{
    if( record === undefined || record === null ){
      let jr:JsonResult = new JsonResult();
      jr.code = 777;
      jr.msg = '数据为空，不可保存！';
      return of(jr);
    }
    if( record.iid === undefined || record.iid === null || record.iid <=0 )
      return this.insert(record);
    else
      return this.update(record);
  }
  
  private insert(record:ContactCompany):Observable<JsonResult>{
    return this.http.post<JsonResult>(this.url.insert,record);
  }

  private update(record:ContactCompany):Observable<JsonResult>{
    return this.http.put<JsonResult>(this.url.update,record);
  }

  
}


// Author：chanchaw
// Generator Version：1.0 - 2019年11月16日 16点46分
// This Model Created：Sat Feb 01 2020 16:19:13 GMT+0800 (中国标准时间)
// 记得导入UUID工具：import { UUID } from 'angular2-uuid';
export class ContactCompany{
  iid:number;
  sid:string;
  company_name:string;
  type_sid:string;
  type_name:string;
  alias:string;
  settlement_sid:string;
  settlement_name:string;
  remark:string;
  seq:number;
  create_time:Date;
  uuid:string;

  constructor(){
    this.uuid = UUID.UUID();
  }
}
